package org.dllwh.alibaba.nacos.plugin.datasource.impl.base;


import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.impl.mysql.ConfigInfoBetaMapperByMySql;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import org.dllwh.alibaba.nacos.plugin.datasource.dialect.DatabaseDialect;
import org.dllwh.alibaba.nacos.plugin.datasource.manager.DatabaseDialectManager;

import java.util.Collections;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName BaseConfigInfoBetaMapper
 * @Description The base implementation of ConfigInfoBetaMapper.
 * @Date 2024-11-23 12:26
 * @版本 V 1.0.1
 * @since JDK 17
 */
public class BaseConfigInfoBetaMapper extends ConfigInfoBetaMapperByMySql {
    
    private DatabaseDialect databaseDialect;
    
    public BaseConfigInfoBetaMapper() {
        databaseDialect = DatabaseDialectManager.getInstance().getDialect(getDataSource());
    }
    
    @Override
    public String getTableName() {
        return TableConstant.CONFIG_INFO_BETA;
    }
    
    public String getLimitPageSqlWithOffset(String sql, int startRow, int pageSize) {
        return databaseDialect.getLimitPageSqlWithOffset(sql, startRow, pageSize);
    }
    
    @Override
    public MapperResult findAllConfigInfoBetaForDumpAllFetchRows(MapperContext context) {
        int startRow = context.getStartRow();
        int pageSize = context.getPageSize();
        String sqlInner = getLimitPageSqlWithOffset("SELECT id FROM config_info_beta  ORDER BY id ", startRow,
                pageSize);
        String sql =
                " SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5,gmt_modified,beta_ips,encrypted_data_key "
                        + " FROM ( " + sqlInner + "  )" + "  g, config_info_beta t WHERE g.id = t.id ";
        return new MapperResult(sql, Collections.emptyList());
    }
    
    
}
